using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;
using System.Web.Security;

namespace VoiceAd.DAL
{
    public class MonitorRepository : IMonitorRepository, IDisposable
    {
        private dbVoiceAdDataContext context = new dbVoiceAdDataContext();

        public MonitorRepository(dbVoiceAdDataContext db)
        {
            this.context = db;
        }

        public IEnumerable<tblMonitor> GetListMonitors()
        {
            return context.tblMonitors.ToList();
        }

        public IEnumerable<tblMonitor> GetListMonitorsByCampId(int campId)
        {
            return context.tblMonitorGetListByCampId(campId).ToList();
        }

        public IEnumerable<tblMonitor> GetListMonitorsByStationId(int campId)
        {
            string strUser = Membership.GetUser().UserName;
            return context.tblMonitorGetListByStationId(campId, strUser).ToList();
        }

        public IEnumerable<tblMonitor> GetListMonitorsByBookingId(int bookingId)
        {
            return context.tblMonitors.Where(x => x.BookingId == bookingId && x.UploadDay <= DateTime.Now).ToList();
        }

        public IEnumerable<tblMonitor> GetListMonitorsByBookingAd(int bookingId)
        {
            return context.tblMonitors.Where(x => x.BookingId == bookingId ).ToList();
        }

        public tblMonitor GetMonitorById(int id)
        {
            return context.tblMonitors.Where(x => x.Id == id).FirstOrDefault();
        }

        public int GetSumMonitorByBookId(int id)
        {
            int? intSum = 0;
            return context.tblMonitorGetSumbyBookId(id, ref intSum);
        }

        public int GetSumMonitorByUploadDay(int id, DateTime dUpload)
        {            
            return context.tblMonitors.Where(x => x.tblBooking.Id == id && x.UploadDay == dUpload).Count();
        }

        public int tblMonitorSumDayByCampId(int id)
        {
            int? intSum = 0;
            context.tblMonitorSumDayByCampId(id, ref intSum);
            return intSum.Value;
        }

        public void InsMonitor(tblMonitor tblmonitor)
        {
            context.tblMonitors.InsertOnSubmit(tblmonitor);
            context.SubmitChanges();
        }

        public void UpdMonitor(tblMonitor tblmonitor)
        {
            tblMonitor obj = context.tblMonitors.Where(x => x.Id == tblmonitor.Id).First();
            obj.BookingId = tblmonitor.BookingId;
            obj.UrlFile = tblmonitor.UrlFile;
            obj.UploadDay = tblmonitor.UploadDay;
            context.SubmitChanges();
        }

        public void DelMonitor(int id)
        {
            tblMonitor obj = context.tblMonitors.Single(x => x.Id == id);
            context.tblMonitors.DeleteOnSubmit(obj);
            context.SubmitChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
